package com.oa.action;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Department;
import com.oa.entity.Post;
import com.oa.entity.User;
import com.oa.service.DepartmentService;
import com.oa.service.PostService;
import com.oa.service.UserService;
import com.oa.util.DataUtil;
import com.oa.util.NodeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
	private static final long serialVersionUID = 1L;
	private User user = new User();
	
	// 通过注解注入 服务
	@Resource(name="userService")
	private UserService userService;
	@Resource(name="postService")
	private PostService postService;
	@Resource(name="departmentService")
	private DepartmentService departmentService;
	
	// 定义属性来 接收 前台部门的数据
	private String departmentId ;
	// 接收前台多个岗位的Id
	private String [] postIds;
	public String getDepartmentId() {
		return departmentId;
	}
	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}
	public String[] getPostIds() {
		return postIds;
	}
	public void setPostIds(String[] postIds) {
		this.postIds = postIds;
	}
	
	
	//========================================================
	

	// 显示 用户列表
	public String list() {
		List<User> userList = userService.list();
		ActionContext.getContext().put("userList", userList);
		return "list";
	}

	// 添加 用户
	public String add() {
		// 设置所属部门
		user.setDepartment(departmentService.getById(departmentId));
		// 设置关联的岗位
		List<Post> roleList = postService.getByIds(postIds);
		user.setPostSet(new HashSet<Post>(roleList));
		// 通过MD5设置默认密码为123456
		String md5Digest = DataUtil.md5("123456");
		user.setUser_password(md5Digest);
		boolean bl = userService.add(user);
		if (bl)
			return "toList";
		else
			return "err";
	}

	// 添加 用户页面
	public String addPage() {
		// 准备数据, departmentList 部门列表
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = NodeUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		// 准备数据, postList 岗位列表
		List<Post> postList = postService.list();
		ActionContext.getContext().put("postList", postList);
		return "savePage";
	}

	// 修改 用户页面
	public String updatePage() {
		
		// 准备数据, departmentList
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = NodeUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);

		// 准备数据, postList
		List<Post> postList = postService.list();
		ActionContext.getContext().put("postList", postList);

		// 准备回显的数据
		User user1 = userService.getById(user.getUnique_id());
		ActionContext.getContext().put("user1", user1);
		if (user1.getDepartment() != null) {
			departmentId = user1.getDepartment().getUnique_id();
		}
		if (user1.getPostSet() != null) {
			postIds = new String[user1.getPostSet().size()];
			int index = 0;
			for (Post post : user.getPostSet()) {
				postIds[index++] = post.getUnique_id();
			}
		}
		
		return "savePage";
	}

	// 删除 用户
	public String delete() {
		boolean bl = userService.delete(user);
		if (bl)
			return "toList";
		else
			return "err";
	}

	// 修改 用户
	public String update() {
		System.err.println(user.getUnique_id()+"...........");
		// 1，从数据库中取出原对象
		User user1 = userService.getById(ServletActionContext.getRequest().getParameter("unique_id"));

		// 2，设置要修改的属性
		user1.setUser_loginName(user.getUser_loginName());
		user1.setUser_name(user.getUser_name());
		user1.setUser_gender(user.getUser_gender());
		user1.setUser_phoneNumber(user.getUser_phoneNumber());
		user1.setUser_email(user.getUser_email());
		user1.setUser_description(user.getUser_description());
		// >> 设置所属部门
		user1.setDepartment(departmentService.getById(departmentId));
		// >> 设置关联的岗位
		List<Post> postList = postService.getByIds(postIds);
		user1.setPostSet(new HashSet<Post>(postList));

		// 3，更新到数据库
		boolean bl = userService.update(user1);

		if (bl)
			return "toList";
		else
			return "err";
	}
	// 初始化密码
	public String initPassword() {
		// 1，从数据库中取出原对象
		User user1 = userService.getById(user.getUnique_id());

		// 2，MD5 加密
		String md5Digest = DataUtil.md5("123456");
		user1.setUser_password(md5Digest);

		// 3，更新到数据库
		boolean bl = userService.update(user1);

		if (bl)
			return "toList";
		else
			return "err";
	}
	
	// 登录月面
	public String loginPage(){
		
		return "loginPage";
	}
	
	// 登录
	public String login(){
		System.out.println(user.getUser_loginName()+user.getUser_password());
		User user1 = userService.findUserNameAndPass(user);
		if(user1==null){
			addFieldError("login", "用户名或者密码不正确！");
			return "loginPage";
		}else{
			ActionContext.getContext().getSession().put("user1", user1);
			return "toIndex";
		}
	}
	
	// 注销
	public String logout(){
		ActionContext.getContext().getSession().remove("user1");
		System.out.println("注销....");
		return "logout";
	}
	
	
	// 返回对象模型
	public User getModel() {
		return user;
	}

}
